package com.epoint.mobileoa.ui.statistics;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.service.MainService;

/**
 * @author liyc
 * @time 2012-5-4 上午10:30:43
 * @annotation 
 */
public class StatisticsView extends SuperView {

	private ListView lv;
	@Override
	public void initControls() {
		lv = (ListView) findViewById(R.id.lvTj);
		lv.setOnItemClickListener(this);
		showDialog_wait(null);
		Task task = new GetStatisticsInfoTask(this, null);
		MainService.addTask(task);
	}

	List<StatisticsModel> list = new ArrayList<StatisticsModel>();
	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			String bs = param[1].toString();
			list = GetStatisticsInfoTask.XmlAnalysis(bs);
			MyAdapter adapter = new MyAdapter();
			lv.setAdapter(adapter);
		}
		m_Dialog.dismiss();
	}

	@Override
	public int getLayoutInt() {
		return R.layout.statisticsview;
	}

	@Override
	public String setTitle() {
		return "移动办公使用统计";
	}

	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater.from(StatisticsView.this);

				convertView = mInflater.inflate(R.layout.chuchaidetaillistview, null);

				holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
				holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
				holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
				holder.tv4 = (TextView) convertView.findViewById(R.id.tv4);
				holder.tv5 = (TextView) convertView.findViewById(R.id.tv5);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if(position>0){
				if(!list.get(position-1).OUNAME.equals(list.get(position).OUNAME)){
					holder.tv1.setBackgroundColor(getResources().getColor(R.color.black));
					holder.tv1.setVisibility(View.VISIBLE);
					holder.tv1.setText(Html.fromHtml("<font color="+getYGColor()+">部门:</font><font color=white>"+list.get(position).OUNAME+"</font>"));
				}
				else{
					holder.tv1.setVisibility(View.GONE);
				}
			}
			else{
				holder.tv1.setVisibility(View.VISIBLE);
				holder.tv1.setBackgroundColor(getResources().getColor(R.color.black));
				holder.tv1.setText(Html.fromHtml("<font color="+getYGColor()+">部门:</font><font color=white>"+list.get(position).OUNAME+"</font>"));
			}
			holder.tv2.setText(Html.fromHtml("<font color="+getYGColor()+">登陆人:</font><font color="+getBlackOrWhiteColor()+">"+list.get(position).DISPLAYNAME+"</font>"));
			holder.tv3.setText(Html.fromHtml("<font color="+getYGColor()+">登陆次数:</font><font color="+getBlackOrWhiteColor()+">"+list.get(position).LOGINTIMES+"</font>"));
			holder.tv4.setText(Html.fromHtml("<font color="+getYGColor()+">设备ID:</font><font color="+getBlackOrWhiteColor()+">"+list.get(position).DEVICEID+"</font>"));
			holder.tv5.setVisibility(View.GONE);
			return convertView;
		}
	}

	public class ViewHolder {
		public TextView tv1;
		public TextView tv2;
		public TextView tv3;
		public TextView tv4;
		public TextView tv5;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}
}
